
from ast import Interactive
import os
import random
import httpx  # 导入 httpx 库
import gradio as gr
import pickle
import pandas as pd

from pandas.core.methods.describe import describe_timestamp_1d

from plot_base import multi_grid_line, multi_macro_bar_or_line, item_timeline_seasonal, xy_match_same_freq_plot

from data_source import get_data_by_meta
from data_process import to_daily, to_monthly_mean, TjdSingleData, gen_xy_same_freq, cal_tztd_index

from utils import load_yaml_file
from typing import Dict
# now = datetime.now().strftime("%Y-%m-%d")

from volcenginesdkarkruntime import Ark
from kaleido.scopes.plotly import PlotlyScope

from docx import Document
from docx.shared import Inches

scope = PlotlyScope()


client = Ark(
    api_key="504bfd41-c285-4924-9107-74bb71846399",
    base_url="https://ark.cn-beijing.volces.com/api/v3",
    timeout=httpx.Timeout(timeout=1800),
)

class MyHtml:
    def __init__(self, html):
        self.html = html

    def ask_llm(self, ask):
        # 请求1
        completion = client.chat.completions.create(
            model="doubao-1-5-pro-256k-250115",
            messages=[
                {"role": "system", "content": "你是一个经济分析师，你会根据给定的数据，给出准确的数据描述。不超过150字"},
                {"role": "user", "content": f"{ask}"},
            ],
        )
        print(completion.choices[0].message.content)
        msg = completion.choices[0].message.content
        return msg

    def show_html(self):
        with gr.Blocks() as demo:
            gr.Markdown("# 经济数据分析助手")
            with gr.Row():
                ask = gr.Text(label='提问', value=f"请帮我文字控制在100字以内", scale=2)
                btn = gr.Button('问问', scale=1)
            answer = gr.Markdown(value="AI数据分析...")

            btn.click(
                fn=self.ask_llm,
                inputs=[ask],
                outputs=[answer]
            )
        return demo


myhtml = MyHtml('')
demo = myhtml.show_html()
demo.launch()